<?php
namespace app\admin\controller;
use app\admin\controller\Common;
class Cate extends Common
{
    public function lst()
    {
        //获取表的前缀
        $prefix= config("database.prefix");
        $modelName=$prefix.'model';
        //连表查询所需要的表名及字段名称
        //获取栏目
        $_cateRes=db('cate')->alias('c')->field('c.*,m.model_name')->join("$modelName m",'c.model_id=m.id')->order('sort desc')->select();
        $cateRes=model('cate')->cateTree($_cateRes);
        $this->assign('cateRes',$cateRes);
        return view();
    }
    public function ajaxLst(){
        if(request()->isAjax()) {
            $cateId = input('cateid');
            $sonIds = model('cate')->childRenids($cateId);
            echo json_encode($sonIds);
        }else{
            $this->error('非法操作!');
        }
    }
    public function add()
    {
        if (request()->isPost()){
            $data=input('post.');
            //验证数据
            $validate=validate('Cate');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $add=db('cate')->insert($data);
            if ($add){
                $this->success('添加栏目成功',url('lst'));
            }
            else{
                $this->error('添加栏目失败');
            }
            return;
        }
        //获取栏目

        $_cateRes=db('cate')->select();
        $cateRes=model('cate')->cateTree($_cateRes);
        $cateId=input('cate_id');
        $modelRes=db('model')->field('id,model_name')->select();
        $this->assign(array(
            'cateRes'=>$cateRes,
            'cateid'=>$cateId,
            'modelRes'=>$modelRes
        ));
        return view();
    }
    public function upimg(){
      $file=request()->file('img');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/cateimg');
        if ($info){
            echo $info->getSaveName();
        }else{
            echo $info->getError();
        }
    }
    //ajax异步修改栏目状态
    public function changeStatus(){
        if(request()->isAjax()){
       $cateid=input('cateid');
        $status=db('cate')->field('status')->where('id',$cateid)->find();
        $status=$status['status'];
        if ($status==1){
            db('cate')->where('id',$cateid)->update(['status'=>0]);
            echo 1;//由显示变隐藏
        }else{
            db('cate')->where('id',$cateid)->update(['status'=>1]);
            echo 2;//由隐藏变显示
        }
        }else{
            $this->error('非法操作!');
        }
    }

    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            //判断是否隐藏
            $_data=array();
            foreach ($data as $k=>$v){
                $_data[]=$k;
            }
            if (!in_array('status',$_data)){
                $data['status']=1;
            }
            //验证数据
            $validate=validate('Cate');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $save = db('cate')->update($data);
            if ($save !== false) {
                $this->success('修改栏目成功', url('lst'));
            } else {
                $this->error('修改栏目失败');
            }
            return;
        }
        $_cateRes=db('cate')->select();
        $cateRes=model('cate')->cateTree($_cateRes);
        $cateId = input('id');
        $cates = db('cate')->find($cateId);
        //模型id
        // $modelIds=db('cate')->column('model_id');
        $modelRes=db('model')->field('id,model_name')->select();
        $this->assign(array(
            'cateRes' => $cateRes,
            'cates' => $cates,
            'modelRes'=>$modelRes,
        ));
        return view();
    }
    public function del_sort(){
        $data=input('post.');
//        foreach ($data['sort'] as $k=>$v){
//            db('cate')->where('id',$k)->update(['sort'=>$v]);
//        }
        if ($data['itm']){
           model('cate')->delAll($data['itm']);
        }
        $this->success('数据处理成功!',url('lst'));
    }
    public function del(){
        $id=input('id');
        $childRenids=model('cate')->childRenids($id);
        $childRenids[]=(int)$id;
       $del= db('cate')->delete($childRenids);
        if ($del){
            $this->success('删除栏目成功!','lst');
        }else{
            $this->error('删除栏目失败!');
        }
    }
    public function delImg(){
        $cateid=input('cateid');
        $imgurl= input('imgurl');
//        $imgUrl=ADMIN_IMG.$imgurl;
        $imgUrl='./uploads/cateimg/'.$imgurl;
        $res=@unlink($imgUrl);
        if ($cateid){
            db('cate')->where('id',$cateid)->setField('img','');
        }
        if ($res){
            echo 1;//删除文件成功
        }else{
            echo 2;//删除文件失败
        }
    }

    public function ajaxCateInfo(){
        $cateId=input('cateid');
        $data=db('cate')->find($cateId);
        echo json_encode($data);
    }
}
